package com.framework.web;

import com.framework.common.util.date.DateUtil;
import com.framework.common.util.http.HttpClientUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author 龘鵺
 * @className com.framework.web
 * @description 1
 * @datetime 2025-01-09 11:11:11
 * @version 1.0
 */
public class HttpClientUtils {
    public static String doPost(String url, Map<String, String> map, String encoding) {
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try {
            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost(url);
            //璁剧疆鍙傛暟
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(), String.valueOf((String) elem.getValue())));
            }
            if (list.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, encoding);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                System.out.println("resEntity:" + resEntity);
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, encoding);
                }
                System.out.println("result:" + result);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        String mchId = "400002001";
        String notify_url = "www.baidu.com";
        String page_url = "www.baidu.com";
        String orderNo = System.currentTimeMillis() + "";
        Date date = new Date();
//        Map<String, Object> map = new TreeMap<String, Object>();
        Map<String, String> map = new TreeMap<String, String>();
        map.put("version", "1.0");//需同步返回JSON 必填，固定值 1.0
        map.put("mch_id", mchId);//商户号 平台分配唯一
        map.put("notify_url", notify_url);//异步通知地址,不超过 200 字节,支付成功后发起,不能携带参数
        map.put("page_url", page_url);//同步跳转地址,	不超过 200 字节,支付成功后跳转地址,不能携带参数
        map.put("mch_order_no", orderNo);//	商家订单号,保证每笔订单唯一
        map.put("pay_type", "");//支付类型,请查阅商户后台通道编码
        map.put("trade_amount", "");//交易金额,当地货币 精确到元
        map.put("order_date", DateUtil.getDateToString(date, null));//订单时间,时间格式(北京时间) yyyy-MM-dd HH:mm:ss
//        map.put("bank_code", "IDPT0001");//银行代码,网银通道必填，其他类型一定不能填该参数
        map.put("goods_name", "");//商品名称,不超过 50 字节
//        map.put("mch_return_msg", "");//透传参数,不超过200字节
//        map.put("payer_phone", "");//手机号码,付款人手机号码,肯尼亚代收必填(手机号前需加上国际区号,例:254798371123
        map.put("sign_type", "MD5");//签名方式,固定值 MD5，不参与签名
//        map.put("sign", "");
        String url = "https://payment.qeaepay.com/pay/web";
//        String dataStr = HttpClientUtil.postString(url, map);
        String dataStr = doPost(url, map, null);
        System.out.println(dataStr);
    }
}
